/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.gui.dialogs.settings;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
	 
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


/**
 *
 * @author Leonhart
 */
public class SaxParser extends DefaultHandler {

    List<XMLSettings> SettingsL;
    String tmpValue;
    XMLSettings settings;
    SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
    private static SaxParser _instance = null;
    
    public static SaxParser GetInstance()
    {
        if(_instance == null) _instance = new SaxParser();
        return _instance;
    }
    
    public SaxParser() {
        SettingsL = new ArrayList<XMLSettings>();
        //parseDocument();
        //printDatas();
    }

    public void parseDocument(String settingsXmlFileName) {
        // parse
        if(!SettingsL.isEmpty())
            SettingsL.clear();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            SAXParser parser = factory.newSAXParser();
            parser.parse(settingsXmlFileName, this);
        } catch (ParserConfigurationException e) {
            System.out.println("ParserConfig error");
        } catch (SAXException e) {
            System.out.println("SAXException : xml not well formed");
        } catch (IOException e) {
            System.out.println("IO error");
        }
    }
public void SaveParameters(ArrayList<XMLSettings> SettingsL)
{
    try{
    PrintWriter FileSettings = new PrintWriter(new BufferedWriter(new FileWriter("c:\\Parameters.xml")));
    FileSettings.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
    FileSettings.println("<Parameters>");
    for(XMLSettings setting : SettingsL)
    {
        FileSettings.println("<Parameter>");
        FileSettings.println("<Name>" + setting.GetNameParameter() + "</Name>");
        FileSettings.println("<Value>"+setting.GetValueParameter()+"</Value>");
        FileSettings.println("</Parameter>");
    }
    FileSettings.println("</Parameters>");
    FileSettings.close();    
    tmpValue = null;
    settings = null;
    _instance.parseDocument("c:\\Parameters.xml");
    }
    catch(Exception exception)
    {
        System.out.println(exception.toString());
    }
}
    public void printDatas() {
        // System.out.println(bookL.size());
        for (XMLSettings tmpB : SettingsL) {
            System.out.println(tmpB.GetNameParameter() + " " + tmpB.GetValueParameter());
        }
    }
    public List<XMLSettings> GetListParameters()
    {
        return SettingsL;
    }
    @Override
    public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
        // if current element is book , create new book
        // clear tmpValue on start of element

        if (elementName.equalsIgnoreCase("Parameter")) {
            settings = new XMLSettings();           
        }       
    }

    @Override
    public void endElement(String s, String s1, String element) throws SAXException {
        // if end of book element add to list
        if (element.equals("Parameter")) {
            SettingsL.add(settings);
        }
        if (element.equalsIgnoreCase("Name")) {
            settings.SetNameParameter(tmpValue);
        }
        if (element.equalsIgnoreCase("Value")) {
            settings.SetValueParameter(tmpValue);
        }                
    }

    @Override
    public void characters(char[] ac, int i, int j) throws SAXException {
        tmpValue = new String(ac, i, j);
    }
    
    public String GetParametersAsString(String NameParameter)
    {
        for(XMLSettings xmlS : SettingsL)
        {
            if(xmlS.GetNameParameter() == null ? NameParameter == null : xmlS.GetNameParameter().equals(NameParameter))
                return xmlS.GetValueParameter();
        }
        return null;
    }
    public int GetParametersAsInt(String NameParameter)
    {
        String stringValue = null;
        int intValue = 0;
        for(XMLSettings xmlS : SettingsL)
        {
            if(xmlS.GetNameParameter() == null ? NameParameter == null : xmlS.GetNameParameter().equals(NameParameter))
                stringValue = xmlS.GetValueParameter();
        }
        if(stringValue != null)
        {
            intValue = Integer.parseInt(stringValue.trim());
           
        }
        return intValue;
    }
    public float GetParametersAsFloat(String NameParameter)
    {
        String stringValue = null;
        float floatValue = 0;
        for(XMLSettings xmlS : SettingsL)
        {
            if(xmlS.GetNameParameter() == null ? NameParameter == null : xmlS.GetNameParameter().equals(NameParameter))
                stringValue = xmlS.GetValueParameter();
        }
        if(stringValue != null)
        {
            floatValue = Float.valueOf(stringValue.trim()).floatValue();
           
        }
        return floatValue;
    }
}


